﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Coding4Fun.Cannon.Phone.Controls
{
	public partial class SettingsView : UserControl
	{
		public SettingsView()
		{
			// Required to initialize variables
			InitializeComponent();

			// yes, i know should be done with databinding
			// was having issues, will fix later
			EndpointUrl.Text = App.ApplicationSettings.EndpointUrl;
			//UseRcCar.IsChecked = App.ApplicationSettings.UseRcCar;
			IsInverseTilt.IsChecked = App.ApplicationSettings.IsInverseTilt;
		}

		public delegate void FinishedEventHandler(object sender, EventArgs e);
		public event FinishedEventHandler Finished;



		private void Save_Click(object sender, RoutedEventArgs e)
		{
			// save settings
			App.ApplicationSettings.EndpointUrl = EndpointUrl.Text;

			if (UseRcCar.IsChecked != null)
			    App.ApplicationSettings.UseRcCar = UseRcCar.IsChecked.Value;

			if (IsInverseTilt.IsChecked != null)
			    App.ApplicationSettings.IsInverseTilt = IsInverseTilt.IsChecked.Value;

			App.ApplicationSettings.Save();
			App.ReloadSettings();

			if (null != Finished)
				Dispatcher.BeginInvoke(Finished, this, new EventArgs());
		}

		private void Cancel_Click(object sender, RoutedEventArgs e)
		{
			if (null != Finished)
				Dispatcher.BeginInvoke(Finished, this, new EventArgs());
		}
	}
}
